﻿var viewStatus = this.lookupController().analysis().status;
var serviceType = ref.serviceType.getValue();
var vm = this.lookupViewModel();
var workLocationValue = ref.workLocation.getValue();
var hasNewBundOffice = workLocationValue.shanghai_New_Bund_Office == 1;
var hasRDC = workLocationValue.rdc_JLR_Office == 1 || workLocationValue.rdc_Warehouse_W == 1;
var hasJingqiao = workLocationValue.shanghai_jinqiao == 1;
var hasOther = workLocationValue.pdc_Suzhou_W == 1
    || workLocationValue.beijing_Office == 1
    || workLocationValue.pdc_Foshan_W == 1
    || workLocationValue.beijing_Academy == 1
    || workLocationValue.pdc_Beijing_W == 1
    || workLocationValue.guangzhou_Office == 1
    || workLocationValue.pdc_Chongqing_W == 1
    || workLocationValue.guangzhou_Academy == 1
    || workLocationValue.cjlr_Hongqiao_W == 1
    || workLocationValue.chengdu_Office == 1
    || workLocationValue.cjlr_Changshu_W == 1;

var hasPDC = workLocationValue.pdc_Suzhou_W == 1
    || workLocationValue.pdc_Foshan_W == 1
    || workLocationValue.pdc_Beijing_W == 1
    || workLocationValue.pdc_Chongqing_W == 1
    || workLocationValue.cjlr_Hongqiao_W == 1
    || workLocationValue.cjlr_Changshu_W == 1

var hasPdcOther = workLocationValue.shanghai_New_Bund_Office == 1
    || workLocationValue.rdc_Warehouse_W == 1
    || workLocationValue.shanghai_jinqiao == 1
    || workLocationValue.beijing_Office == 1
    || workLocationValue.beijing_Academy == 1
    || workLocationValue.guangzhou_Office == 1
    || workLocationValue.guangzhou_Academy == 1
    || workLocationValue.chengdu_Office == 1
    || workLocationValue.rdc_JLR_Office == 1

var companyResourcesValue = ref.companyResources.getValue();

//不可以申请门禁卡之后，Access Card Start Date和Access Card End Date均应该灰掉
var hasAccessCard = companyResourcesValue.accessCard == 1;

ref.cardStartDate.setAllowBlank(!hasAccessCard);
ref.cardEndDate.setAllowBlank(!hasAccessCard);
ref.newBundFloors.setAllowBlank(!hasNewBundOffice);
if (hasNewBundOffice == true && self.containsFocus) {
    ref.newBundFloors.setAllowBlank(!hasAccessCard);
    ref.newBundFloors.setDisabled(!hasAccessCard);
}

//除JV Partner以外，其他服务类型的人员没有勾选CDSID时，不能申请Skype Account、JLR Family、Email
if (serviceType != '4') {
    if (companyResourcesValue.cdsid == 0) {
        Ext.each(email, function (ctr) {
            ctr.setDisabled(ctr.inputValue != 'Neither');
        });
    }
}
var activity = vm.get('NebulogyForm.activity');

ref.attachment.setHidden(companyResourcesValue.accessCard != 1);

var companyResourcesItems = ref.companyResources.items.items;

if (activity == 'Administration Department Head' || activity.startsWith('Access card application')) {
    ref.cardStartDate.setAllowBlank(true);
    ref.cardEndDate.setAllowBlank(true);
    var hasAccessCard = companyResourcesValue.accessCard == 1;
    if (self.containsFocus) {
        //先保存时间数据.

        if (!vm.get('oldcardStartDate')) {
            vm.set('oldcardStartDate', ref.cardStartDate.getValue());
            vm.set('oldcardEndDate', ref.cardEndDate.getValue());
        }
        if (companyResourcesValue.none_ == 1 || companyResourcesValue.accessCard == 0) {

            // Ext.each(companyResourcesItems, function (ctr) {
            //     ctr.setValue(false);
            //     ctr.setDisabled(false);
            //     if (companyResourcesValue.none_ == 1) {
            //         if (ctr.inputValue != 'none_') {
            //             ctr.setDisabled(true);
            //             ctr.setValue(false);
            //         }
            //     } else if (ctr.inputValue == 'cdsid' || ctr.inputValue == 'skypeAccount' || ctr.inputValue == 'jlrFamily') {
            //         ctr.setDisabled(true);
            //     }
            // });          

            //vm.set('NebulogyForm.companyResourcesRequest.cardStartDate', null);
            //vm.set('NebulogyForm.companyResourcesRequest.cardEndDate', null);
            if (!hasAccessCard && self.containsFocus) {
                vm.set('NebulogyForm.companyResourcesRequest.cardStartDate', '');
                vm.set('NebulogyForm.companyResourcesRequest.cardEndDate', '');
            };

        } else if (companyResourcesValue.accessCard == 1) {

            vm.set('NebulogyForm.companyResourcesRequest.cardStartDate', vm.get('oldcardStartDate'));
            vm.set('NebulogyForm.companyResourcesRequest.cardEndDate', vm.get('oldcardEndDate'));
        }

        ref.accept.setHidden(companyResourcesValue.accessCard == 0);
    }
} else if (viewStatus == 'launch' || viewStatus == 'resubmit') {
    //楼层控制.
    //ref.newBundFloors.setDisabled(!hasNewBundOffice);
    if (!hasNewBundOffice) {
        Ext.each(ref.newBundFloors.items.items, function (ctr) {
            ctr.setValue(false);
        });
    } else {
        //先禁用楼层的Facial Recognition
        Ext.each(ref.newBundFloors.items.items, function (ctr) {
            if (ctr.inputValue == 'facialRecognition') {
                ctr.setDisabled(true);
                //如果包含NewBundOffice并且 Service Type = 2(Long-term Outsourcing Service) 和 4 (JV Partner),Facial Recognition才可以选中.
                if (hasNewBundOffice && (serviceType == 2 || serviceType == 4)) {
                    ctr.setDisabled(false);
                }
            }
        });
    }

    if (companyResourcesValue.none_ == 1) {
        //如果选择None，则Company Resource其他项都灰掉.
        Ext.each(companyResourcesItems, function (ctr) {
            if (ctr.inputValue != 'none_') {
                ctr.setDisabled(true);
                ctr.setValue(false);
            }
        });
        ref.cardStartDate.setAllowBlank(true);
        ref.cardStartDate.setDisabled(true);

        ref.cardEndDate.setAllowBlank(true);
        ref.cardEndDate.setDisabled(true);

        vm.set('NebulogyForm.companyResourcesRequest.cardStartDate', '');
        vm.set('NebulogyForm.companyResourcesRequest.cardEndDate', '');

    } else {
        Ext.each(companyResourcesItems, function (ctr) {
            if (ctr.inputValue != 'none_') {
                ctr.setDisabled(false);
            }
        });
        var email = ref.email.items.items;
        var cardStartDate = ref.cardStartDate.getValue();
        var serviceStart = ref.serviceStart.getValue();

        //不可以申请门禁卡之后，Access Card Start Date和Access Card End Date均应该灰掉
        var hasAccessCard = companyResourcesValue.accessCard == 1;

        ref.cardStartDate.setAllowBlank(!hasAccessCard);
        ref.cardStartDate.setDisabled(!hasAccessCard);
        ref.cardEndDate.setAllowBlank(!hasAccessCard);
        ref.cardEndDate.setDisabled(!hasAccessCard);
        if (!hasAccessCard && self.containsFocus) {
            vm.set('NebulogyForm.companyResourcesRequest.cardStartDate', '');
            vm.set('NebulogyForm.companyResourcesRequest.cardEndDate', '');
        };

        ref.email.setValue("");

        switch (serviceType) {
            case '1':
                //companyResources 可选项控制
                Ext.each(companyResourcesItems, function (ctr) {
                    ctr.setDisabled(false);

                    if (ctr.inputValue != 'cdsid' && ctr.inputValue != 'accessCard' && ctr.inputValue != 'none_') {
                        ctr.setDisabled(true);
                        ctr.setValue(false);
                    }
                    if (hasPDC && !hasPdcOther) {
                        if (ctr.inputValue == 'accessCard') {
                            ctr.setDisabled(true);
                        }
                    }
                });

                Ext.each(email, function (ctr) {
                    ctr.setDisabled(ctr.inputValue != 'Neither');
                });

                // if (cardStartDate && hasAccessCard) {
                //     ref.cardEndDate.setValue(Ext.Date.add(cardStartDate, Ext.Date.MONTH, 6));
                //     ref.cardEndDate.setMaxValue(Ext.Date.add(cardStartDate, Ext.Date.MONTH, 6));
                // }
                break;
            case '2':
                //companyResources 可选项控制
                Ext.each(companyResourcesItems, function (ctr) {
                    ctr.setDisabled(false);
                    switch (ctr.inputValue) {
                        case 'accessCard':
                            if (hasPDC && !hasPdcOther) {
                                ctr.setDisabled(true);
                            }
                            break;
                        case 'skypeAccount':
                            if (hasOther && (!hasNewBundOffice && !hasRDC && !hasJingqiao)) {
                                ctr.setDisabled(true);
                                ctr.setValue(false);
                            }
                            break;
                        case 'locker':
                            if ((hasRDC || hasJingqiao || hasOther) && !hasNewBundOffice) {
                                ctr.setDisabled(true);
                                ctr.setValue(false);
                            }
                            break;
                        default:
                    }
                });
                //email 控制
                Ext.each(email, function (ctr) {
                    ctr.setDisabled(false);

                    if (ctr.inputValue == 'IMSS' && hasRDC && !hasNewBundOffice && !hasJingqiao && !hasOther) {
                        ctr.setDisabled(true);
                    }
                });

                // if (cardStartDate && hasAccessCard) {
                //     ref.cardEndDate.setValue(Ext.Date.add(cardStartDate, Ext.Date.YEAR, 1));
                //     ref.cardEndDate.setMaxValue(Ext.Date.add(cardStartDate, Ext.Date.YEAR, 1));
                // }
                break;
            case '3':
                //companyResources 可选项控制
                Ext.each(companyResourcesItems, function (ctr) {
                    ctr.setDisabled(false);
                    switch (ctr.inputValue) {
                        case 'accessCard':
                            if (hasPDC && !hasPdcOther) {
                                ctr.setDisabled(true);
                            }
                            break;
                        case 'skypeAccount':
                            if (hasOther && (!hasNewBundOffice && !hasRDC && !hasJingqiao)) {
                                ctr.setDisabled(true);
                                ctr.setValue(false);
                            }
                            break;
                        case 'locker':
                            if ((hasRDC || hasJingqiao || hasOther) && !hasNewBundOffice) {
                                ctr.setDisabled(true);
                                ctr.setValue(false);
                            }
                            break;
                        default:
                    }
                });

                //email 控制
                Ext.each(email, function (ctr) {
                    ctr.setDisabled(false);

                    if (ctr.inputValue == 'IMSS' && hasRDC && !hasNewBundOffice && !hasJingqiao && !hasOther) {
                        ctr.setDisabled(true);
                    }
                });
                // if (cardStartDate && hasAccessCard) {
                //     ref.cardEndDate.setValue(Ext.Date.add(cardStartDate, Ext.Date.MONTH, 6));
                //     ref.cardEndDate.setMaxValue(Ext.Date.add(cardStartDate, Ext.Date.MONTH, 6));
                // }
                break;
            case '4':
                //companyResources 可选项控制
                Ext.each(companyResourcesItems, function (ctr) {
                    ctr.setDisabled(false);
                    if (ctr.inputValue == 'cdsid') {
                        ctr.setDisabled(true);
                        ctr.setValue(false);
                    } else {
                        switch (ctr.inputValue) {
                            case 'accessCard':
                                if (hasPDC && !hasPdcOther) {
                                    ctr.setDisabled(true);
                                }
                                break;
                            case 'skypeAccount':
                                if ((hasRDC || hasOther) && (!hasNewBundOffice && !hasJingqiao)) {
                                    ctr.setDisabled(true);
                                    ctr.setValue(false);
                                }
                                break;
                            case 'jlrFamily':
                            case 'accessCard':
                                if (hasRDC && (!hasNewBundOffice && !hasJingqiao && !hasOther)) {
                                    ctr.setDisabled(true);
                                    ctr.setValue(false);
                                }
                                break;
                            case 'locker':
                                if ((hasRDC || hasJingqiao || hasOther) && !hasNewBundOffice) {
                                    ctr.setDisabled(true);
                                    ctr.setValue(false);
                                }
                                break;
                            default:
                        }
                    }
                });

                //email 控制
                if (hasRDC && !hasNewBundOffice && !hasJingqiao && !hasOther) {
                    Ext.each(email, function (ctr) {
                        ctr.setDisabled(ctr.inputValue != 'Neither');
                    });
                    ref.email.setValue('');
                } else {
                    Ext.each(email, function (ctr) {
                        ctr.setDisabled(false);
                        if (ctr.inputValue == 'JLR') {
                            ctr.setDisabled(true);
                        }
                    });
                }

                //门禁卡验证.
                // if (!hasRDC && cardStartDate && hasAccessCard) {
                //     ref.cardEndDate.setValue(Ext.Date.add(cardStartDate, Ext.Date.YEAR, 1));
                //     ref.cardEndDate.setMaxValue(Ext.Date.add(cardStartDate, Ext.Date.YEAR, 1));
                // }
                break;
        }
    }
}
// 设置smartDs中companyResources值，审批，提交，重新提交都需要设值
var companyResources = [];

Ext.each(companyResourcesItems, function (ctr) {
    if (ctr.checked) {
        companyResources.push(ctr.boxLabel);
    }
});
vm.set('NebulogyForm.companyResourcesStr', companyResources.join(','));
